const {
  MemberModel
} = require('../../../utils/member-model.js')
const memberModel = new MemberModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIntegral: 0, //累计经验值
    itemData: [], //成长记录列表
    isLoadData: false,
    isHints: false,
    pageIndex: 1,
    pageSize: 20,
    pageCount: 1,
    loadText: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      currentIntegral: options.currentIntegral
    })
    this.userExpList();
  },

  /**
   * 成长记录
   */
  userExpList: function() {
    let param = {
       merchantId: wx.getStorageSync('merchantId'),
      "offset": (this.data.pageIndex - 1) * this.data.pageSize,
      "limit": this.data.pageSize
    }
    memberModel.userExpList(param).then(data => {
      if (data) {
        let pageCountTotal = data.totalCount % this.data.pageSize == 0 ? (data.totalCount / this.data.pageSize) : (parseInt(data.totalCount / this.data.pageSize) + 1);
        this.setData({
          pageCount: pageCountTotal
        })
        if (data.totalCount > 0) {
          var dataList = [];
          for (var i = 0; i < data.data.length; i++) {
            var item = data.data[i];
            var list = {};
            list.shopName = item.shopName || ""; //门店名称
            list.amount = item.amount || 0; //经验值数额
            list.changeType = (item.changeFlag == 0 || item.changeFlag == 2) ? "+" : (item.changeFlag == 1 || item.changeFlag == 3) ? "-" : ""; //资产变化：0: 增加, 1: 减少 2: 增加的冻结 3: 减少的冻结
            list.remark = item.remark || ""; //备注
            list.createTime = item.createTime ? item.createTime.replace(/-/g, "/") : ""; //时间
            dataList.push(list);
          };
          // 将新一页的数据添加到原数据后面
          var userDataList = this.data.itemData;
          var newData = userDataList.concat(dataList);
          this.setData({
            isLoadData: false,
            isHints: false,
            itemData: newData
          })
        } else {
          this.setData({
            loadText: "",
            isLoadData: false,
            isHints: true,
            itemData: []
          })
        }
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },

  /**
   * 滚动到底部/右边时触发
   */
  scrollEnd: function() {
    this.setData({
      pageIndex: this.data.pageIndex + 1,
      isLoadData: true,
      loadText: "加载中..."
    }, function() {
      if (this.data.pageIndex > this.data.pageCount) {
        this.setData({
          isLoadData: true,
          loadText: "已经到底了"
        });
      } else {
        this.userExpList();
      }
    })
  }

})